﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using ResxHelper.Object;
using ResxHelper.Logic.Readers;
using ResxHelper.Logic.Actions;

namespace ResxHelper.Logic.Converter
{
    public class ConvertBulkIOS : BulkConverterBase
    {
        private const string ResourceTopLevelDirectory = ".lproj";

        public ConvertBulkIOS()
        {
            SaveFileType = SaveFileType.IOSResource;
        }

        #region -- Resource To Excel --

        public override void ToExcel(string inputPath, string outputPath)
        {
            // create temp directory
            WorkingDirectory = ExtractZipToTempDirectory(inputPath);

            // read all resource files
            var resourceFiles = ReadDirectories(WorkingDirectory.FullName, ResourceTopLevelDirectory);

            // save temp excel files
            var tempPaths = SaveTempExcelFiles(WorkingDirectory.FullName, resourceFiles);

            // create a new zip
            var zipRequest = new SaveZipRequest(tempPaths, outputPath, true);
            SaveActionInvoker.Save(zipRequest, SaveFileType.Zip, outputPath);

            // delete the temp directory
            Directory.Delete(WorkingDirectory.FullName, true);
        }

        protected override IEnumerable<ResourceFile> ReadDirectoryFiles(string path)
        {
            var resourceFiles = new List<ResourceFile>();
            foreach (var filePath in Directory.GetFiles(path)
                .Where(f => Path.GetExtension(f) == ".strings"))
            {
                var reader = ResourceFileReaderFactory.GetReader(filePath, ResourceType.IOS);
                resourceFiles.Add(reader.Read());
            }

            return resourceFiles;
        }

        protected override string GetExportFileName(ResourceFile file)
        {
            var fileNameNoExt = file.GetFileNameWithoutExtension();
            var culture = file.CultureInfo.Name;

            var exportName = string.Format("{0}.{1}.xls", fileNameNoExt, culture);
            return exportName;
        }
        
        #endregion

        #region -- Excel To Resource --

        public override void ToResource(string inputPath, string outputPath)
        {
            var tempDirectory = ExtractZipToTempDirectory(inputPath);

            // read all files to resource files
            var resourceFiles = ReadExcelFiles(tempDirectory.FullName);

            // save temp files to proper structure
            var savedPath = SaveToStructure(tempDirectory.FullName, resourceFiles);

            // save zip
            SaveZipDirectory(outputPath, savedPath);

            // cleanup
            Directory.Delete(tempDirectory.FullName, true);
        }

        protected override string GetFilePathFromFileName(string fileName)
        {
            // filename.locale
            var fileNameSplit = fileName.Split('.');
            var name = fileNameSplit[0];
            var locale = fileNameSplit[1];

            var directory = string.Format("{0}.lproj", locale);
            var filePath = string.Format("{0}\\{1}.strings", directory, name);

            return filePath;
        }

        #endregion
    }
}
